<?php

require_once(APPPATH.'libraries/stringutils.php');
require_once("././resources/lib/HTMLPurifier.auto.php");

/**
 * Page: generate html components for a page such as: body, left pan, right pant, ect.
 */
class Page extends CI_Controller
{
	private static $_instance;
	
	function __construct()
	{
		parent::__construct();
		self::$_instance =& $this;
		
		$this->load->model('product_model');
	}
	
	public function getInstance()
	{
		if (self::$_instance == NULL)
			self::$_instance = new Page();
		return self::$_instance;
	}
	
	public function openMainPageTag()
	{
		echo "<div id='mainPan'>";
	}
	
	public function closeMainPageTag()
	{
		echo "</div>";
	}
	
	/*
		Generate page's header
	*/
	public function generateHeader($title)
	{
		?>
		<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
		<title><?php echo $title ?></title>
        
		<link href='<?php echo base_url() ?>resources/css/style.css' rel='stylesheet' type='text/css' />
		
		<?php
		echo script_tag('resources/js/jquery-1.4.2.min.js');
		echo script_tag('resources/js/jquery.easing.min.js');
		echo script_tag('resources/js/jquery.lavalamp.js');
		echo script_tag('resources/js/leftmenu.js');
		echo script_tag('resources/js/core.js');
	}
	
	/*
		Generate left panel
	*/
	public function generateLeftPan()
	{
		?>
		<div id='leftPan'> <!-- Open leftPan tag -->
		
	  	<div id='leftTopPan'>
	  		<?php
	  			$img = array(	'src'	=> base_url()."resources/images/logo.png",
								'title'	=> '',
								'title'	=> '',
								'width'	=> 	160,
								'height'=>	39,
								'border'=>	0,
								"onerror"=> "this.src='".base_url()."resources/products/noimage.jpg'");
				echo img($img);
	  		?>
		</div>
		
		<div class='leftMenuContainer'> <!-- Open leftMenuContainer tag -->
        	<div class='arrowsidemenu'> <!-- Open arrowsidemenu tag -->
		        <div>
		        	<?php echo anchor(site_url(), "Trang chủ") ?>
		        </div>
       
       	<?php
        $categories = Category_Model::getInstance()->getStorageCategories();
		foreach ($categories as $category)
		{
			$productTypes = $category->getProductTypes();
			$this->generateMenuHeader($category);
			$this->generateMenuContent($productTypes);
		}
        ?>
                 
       		</div> <!-- Close arrowsidemenu tag -->
      	</div> <!-- Close leftMenuContainer tag -->
    	</div> <!-- Close leftPan tag -->
    	<?php
	}
	
	/*
		Generate Menu Header: input category's title
	*/
	private function generateMenuHeader($category)
	{
	?>
		<div class='menuheaders'>
			<?php echo anchor(base_url()."category/view/".$category->getId(), $category->getTitle(), array("title"	=> $category->getTitle())) ?>
		</div>
	<?php
	}
	
	/*
		Generate Product Type list: input Product Types
	*/
	private function generateMenuContent($productTypes)
	{
		if (count($productTypes) > 0)
		{
		?>
			<ul class='menucontents'> <!-- Open menucontents tag -->
			
			<?php
			foreach ($productTypes as $productType)
			{
				echo "<li>".anchor('producttype/view/'.$productType->getId(), $productType->getTitle())."</li>";
			}
			?>
			
			</ul> <!-- Close menucontents tag -->
		<?php
		}
	}
	
	/*
		Generate right panel
	*/
	public function generateRightPan($category, $productType = NULL, $products, $paging_link)
	{
	?>
		<div id='rightPan'> <!-- Open rightPan tag -->
  		<?php $this->generateBanner()?>
		
		<!-- TOP MENU-->
	  	<?php $this->generateTopMenu(PRODUCT_TAB)?>
	
		<!-- END TOP MENU -->
		<?php $this->generateCenter($category, $productType, $products, $paging_link) ?>
    
 		</div> <!-- Close rightPan tag -->
 	<?php
	}
	
	public function generateProductType_ProductRightPan($category, $productType = NULL, $products, $paging_link, $error = ERROR_NONE)
	{
	?>
		<div id='rightPan'> <!-- Open rightPan tag -->
  		<?php $this->generateBanner() ?>
		
		<!-- TOP MENU-->
	  	<?php $this->generateTopMenu(PRODUCT_TAB) ?>
	
		<!-- END TOP MENU -->
		<?php $this->generateProductType_ProductCenter($category, $productType, $products, $paging_link, $error) ?>
    
 		</div> <!-- Close rightPan tag -->
 	<?php
	}
	
	public function generateHompageRightPan()
	{
		?>
		<div id='rightPan'> <!-- Open rightPan tag -->
  		<?php $this->generateBanner() ?>
		
		<!-- TOP MENU-->
	  	<?php $this->generateTopMenu(INTRODUCTION_TAB) ?>
	
		<!-- END TOP MENU -->
		<?php $this->generateHomepageCenter() ?>
    
 		</div> <!-- Close rightPan tag -->
 	<?php
	}
	
	public function generateCategoryRightPan()
	{
	?>
		<div id='rightPan'> <!-- Open rightPan tag -->
  		<?php $this->generateBanner() ?>
		
		<!-- TOP MENU-->
	  	<?php $this->generateTopMenu(PRODUCT_TAB) ?>
	
		<!-- END TOP MENU -->
		<?php $this->generateCategoryCenter() ?>
    
 		</div> <!-- Close rightPan tag -->
 	<?php
	}
	
	public function generateNewsRightPan($newsarr, $paginationLinks, $promotion = 0)
	{
	?>
		<div id='rightPan'> <!-- Open rightPan tag -->
  		<?php $this->generateBanner() ?>
		
		<!-- TOP MENU-->
	  	<?php $this->generateTopMenu($promotion ? PROMOTION_TAB : NEWS_TAB) ?>
	
		<!-- END TOP MENU -->
		<?php $this->generateNewsCenter($newsarr, $paginationLinks, $promotion) ?>
    
 		</div> <!-- Close rightPan tag -->
 	<?php	
	}
	
	public function generateCategory_ProductTypeRightPan($category, $producttypes, $error = ERROR_NONE)
	{
	?>
		<div id='rightPan'> <!-- Open rightPan tag -->
  		<?php $this->generateBanner() ?>
		
		<!-- TOP MENU-->
	  	<?php $this->generateTopMenu(PRODUCT_TAB) ?>
	
		<!-- END TOP MENU -->
		<?php $this->generateCategory_ProductTypeCenter($category, $producttypes, $error) ?>
    
 		</div> <!-- Close rightPan tag -->
 	<?php
	}
	
	public function	generateRightPanProductDetail($category = NULL, $producttype = NULL, $product = NULL, $related_products = NULL, $error = ERROR_NONE)
	{
	?>
		<div id='rightPan'> <!-- Open rightPan tag -->
  		<?php $this->generateBanner() ?>
		
		<!-- TOP MENU-->
	  	<?php $this->generateTopMenu(PRODUCT_TAB) ?>
	
		<!-- END TOP MENU -->
		<?php $this->generateCenterProductDetail($category, $producttype, $product, $related_products, $error) ?>
    
 		</div> <!-- Close rightPan tag -->
 	<?php
	}	
	
	public function generateContactInfoRightPan($info)
	{
	?>
		<div id='rightPan'> <!-- Open rightPan tag -->
  		<?php $this->generateBanner() ?>
		
		<!-- TOP MENU-->
	  	<?php $this->generateTopMenu(CONTACT_TAB) ?>
	
		<!-- END TOP MENU -->
		
		<div class='center'>  <!-- Open center tag -->
			<div id='bodyContainer'> <!-- Open bodyContainer tag -->
		
		<?php		
		// Navigation bar
		$link = "<div class='crumb_nav'>".anchor('', 'Home');
		$link .= " &gt;&gt; Liên hệ";
		
		$link .= "</div>";
		
		echo $link;
		echo StringUtils::getInstance()->decodeHTMLString($info->getInfo());		
		?>
		
			<div class='clear'></div>
			</div>
		</div>	<!-- Close center tag -->
    
 		</div> <!-- Close rightPan tag -->
 	<?php
	}
	
	public function generateNewsDetailRightPan($news, $related_news, $error = ERROR_NONE)
	{
	?>
		<div id='rightPan'> <!-- Open rightPan tag -->
  		<?php $this->generateBanner() ?>
		
		<!-- TOP MENU-->
	  	<?php $this->generateTopMenu($news->isPromotion() ? PROMOTION_TAB : NEWS_TAB) ?>
	
		<!-- END TOP MENU -->
		
		<div class='center'>  <!-- Open center tag -->
			<div id='bodyContainer'> <!-- Open bodyContainer tag -->
		
		<?php		
		if ($news->getId() > 0)
		{
		// Navigation bar
		$link = "<div class='crumb_nav'>".anchor('', 'Home');
		$params = $news->isPromotion() ? '/1/0' : '';
		$link .= " &gt;&gt; ".anchor('news'.$params, $news->isPromotion() ? 'Khuyến mãi' : 'Làm đẹp');
		$link .= " &gt;&gt; ".$news->getTitle();
		
		$link .= "</div>";
		
		echo $link;
		
		?>
		<h2><?php echo $news->getTitle() ?></h2>
		<?php
		echo StringUtils::getInstance()->decodeHTMLString($news->getContent());
		
			if (count($related_news) > 0)
			{
		?>
       	<div id="moreNews">
		<h3>Các tin khác</h3>
		<ul>
		<?php
				foreach($related_news as $item)
				{
					?>
					<li>
					<?php
					echo anchor('news/view/'.$item->getId(), $item->getTitle());
					?>
					</li>
					<?php
				}
		?>
		</ul>
        </div>
		<?php
			}
		}
		else
		{
			if ($error == NOT_EXISTED_ERROR)
			{
				?>
				<h2 style="color: red"><?php echo NOT_EXISTED_ERROR_TEXT ?></h2>
				<?php
			}
		}	
		?>
		
			<div class='clear'></div>
		</div>
        <div id="rightContainer"><?php $this->generateRightPaneInfo() ?></div>
    		<div class="both"></div>
		</div> <!-- Close center tag -->
    
 		</div> <!-- Close rightPan tag -->
 	<?php
	}
	
	private function generateBanner()
	{
	?>
		<div id='banner'><div id="face"></div><h1></h1></div>
	<?php
	}
	
	private function generateTopMenu($tabid = 0)
	{
	?>
		<div id='menu'>
    		<ul class="lavaLamp">
            	<li class='<?php echo $tabid == INTRODUCTION_TAB ? "active" : "" ?>'><?php echo anchor(site_url(), 'Trang chủ', array("accesskey" => 1)) ?></li>
            	<li class='<?php echo ($tabid == PRODUCT_TAB) ? "active" : "" ?>'><?php echo anchor(base_url()."category", 'Sản phẩm', array("accesskey" => 2)) ?>
		
				<ul>
		<?php	
		$categories = Category_Model::getInstance()->getStorageCategories();
		 foreach ($categories as $category)
		 {
		 ?>
		 			<li class=''><?php echo anchor('category/view/'.$category->getId(), $category->getTitle()) ?>
						<ul>
				<?php
				foreach ($category->getProductTypes() as $productType)
				{
				?>
							<li class=''><?php echo anchor('producttype/view/'.$productType->getId(), $productType->getTitle()) ?>
							</li>
				<?php
				}
				?>
						</ul>
					</li>
		<?php
		 }
	   	?>
       			</ul>
	       </li>
           		<li class='<?php echo $tabid == PROMOTION_TAB ? "active" : "" ?>'><?php echo anchor(base_url()."news/1/0", 'Khuyến mãi', array("accesskey" => 3)) ?></li>
	            <li class='<?php echo $tabid == NEWS_TAB ? "active" : "" ?>'><?php echo anchor(base_url()."news", 'Làm đẹp', array("accesskey" => 3)) ?></li>	            
	            <li class='<?php echo $tabid == CONTACT_TAB ? "active" : "" ?>'><?php echo anchor(base_url()."info", 'Liên hệ', array("accesskey" => 5))  ?></li>
			</ul>
    	</div>
   	<?php
	}

	private function generateNavigationLink($category, $productType = NULL, $product = NULL)
	{
		$link = "<div class='crumb_nav'>".anchor('', 'Home');
		
		if ($productType !== NULL)
		{
			$link .= " &gt;&gt; ".anchor('category/view/'.$category->getId(), $category->getTitle());
			$link .= " &gt;&gt; ".$productType->getTitle();
		}
		else
		{
			if($category != NULL){
				$link .= " &gt;&gt; ".$category->getTitle();
			}
		}
		
		$link .= "</div>";
		
		echo $link;
	}
	private function generateProductType_ProductNavigationLink($category, $productType = NULL)
	{
		$link = "<div class='crumb_nav'>".anchor('', 'Home');
		
		if ($productType !== NULL)
		{
			$link .= " &gt;&gt; ".anchor('category/view/'.$category->getId(), $category->getTitle());
			$link .= " &gt;&gt; ".$productType->getTitle();
		}
		else
		{
			$link .= " &gt;&gt; ".$category->getTitle();
		}
		
		$link .= "</div>";
		
		echo $link;
	}
	
	private function generateCategory_ProductTypeNavigationLink($category)
	{
		$link = "<div class='crumb_nav'>".anchor('', 'Home');
		$link .= " &gt;&gt; ".$category->getTitle();
		
		$link .= "</div>";
		
		echo $link;
	}
	
	private function generateNavigationLinkProductDetail($category, $productType, $product)
	{
		$link = "<div class='crumb_nav'>".anchor('', 'Home');
		
		if ($productType !== NULL)
		{
			$link .= " &gt;&gt; ".anchor('category/view/'.$category->getId(), $category->getTitle());
			$link .= " &gt;&gt; ".anchor('producttype/view/'.$productType->getId(),$productType->getTitle());
			if($product !== NULL)
			{
				$link .= " &gt;&gt; ".anchor('producttype/view/'.$productType->getId(), $productType->getTitle());
				$link .= " &gt;&gt; ".$product->getName();
			}
			else {
				$link .= " &gt;&gt; ".$productType->getTitle();
			}
		}
		else
		{
			$link .= " &gt;&gt; ".$category->getTitle();
		}
		
		$link .= "</div>";
		
		echo $link;
	}
	
	private function generateListProducts($products, $numberItemPerRow = 4, $showInfo = false){
		$i = 1;
		$numOpenDivTag = 0;
		$numCloseDivTag = 0;
		foreach ($products as $product)
		{
			if($i == 1){
				echo '<div class="row">';
				$numOpenDivTag++;
			}
			
			$this->generateProduct($product, $showInfo);
			if($i == $numberItemPerRow){
				echo '<div class="clear"></div></div>';
				$i = 0;
				$numCloseDivTag++;
			}
			$i++;			
		}
		if($numOpenDivTag != $numCloseDivTag){
			echo '<div class="clear"></div></div>';
		}
	}

	private function generateCenter($category, $productType = NULL, $products, $paging_link)
	{
	?>
		<div class='center'> <!-- Open center tag -->
			<div id='bodyContainer'> <!-- Open bodyContainer tag -->
		
		<?php
		if ($category !== NULL)
		{
			$this->generateNavigationLink($category, $productType); // Navigation bar
			
			if (count($products) > 0)
			{
				?>
				<h2>Danh mục sản phẩm</h2>
				<?php
				
				$this->generateListProducts($products);
			}
			else
			{
				?>
				<p>Không có sản phẩm</p>
				<?php
			}
		}
		else
		{
			?>
			<p>Không có sản phẩm</p>
			<?php
		}
		?>
		
		<!-- Paging here -->
		<?php echo $paging_link ?>
		
			<div class='clear'></div>
			</div>
            <div id="rightContainer"><?php $this->generateRightPaneInfo() ?></div>
    		<div class="both"></div>
		</div> <!-- Close center tag -->
	<?php
	}
	
	private function generateHomepageCenter()
	{
		?>
		<div class='center'> <!-- Open center tag -->
			<div id='bodyContainer'> <!-- Open bodyContainer tag -->
		<?php
		$this->generateNavigationLink(null); // Navigation bar
		?>
        <div class="hompage_intro">
            <h2>Giới thiệu</h2>
            <div>
            <p>
&nbsp&nbsp&nbsp&nbsp Xuất thân từ một tín đồ nghiện mỹ phẩm, mong muốn được sống cả ngày trong biển mỹ phẩm, được nhìn thấy vẻ mặt  thích thú của những bạn gái khi cầm món mỹ phẩm trên tay giống mình của ngày xưa nên <b>Mirror Comestic</b> ra đời. Shop cung cấp mỹ phẩm xách tay nổi tiếng tại USA, Japan, Korean…và đảm bảo nguồn gốc của những sản phẩm này hoàn toàn là hàng thật được ship trực tiếp từ nước ngoài về. Mirror ra đời không phải chỉ muốn kinh doanh ngày một ngày hai mà mong muốn sẽ là địa chỉ gắn liền với bạn suốt một chặng đường dài, nên hãy đặt niềm tin của bạn vào shop, hãy cho shop sự tin tưởng của bạn, shop sẽ đáp lại bạn sự hài lòng, sự thoả mãn. <br /></p>
<p>
<b>Mirror Comestic</b> ra đời với slogan <b>Beauty, beauty and so much beauty</b>, hãy đẹp hơn lên nhé các bạn gái, hãy tìm kiếm cho mình những cách thức kiến mình đẹp lên, tự tin lên không phải để cho người khác ngắm mà để khi nhìn vào gương bạn cười thích thú với chính bạn, hứng khởi với chính bạn. Hãy cùng làm đẹp cho bản thân nhé. <br /></p>
<p>
Trân trọng!
</p>
</div>

        </div><!-- end div intro -->
        <div class="hompage_product_box">
        	<h2><?php echo anchor(base_url()."category", 'Sản phẩm') ?></h2>
        
        <?php
		$products = $this->product_model->getRandomProducts(3);
		if(count($products) > 0)
		{
			foreach($products as $product)
			{
				$this->generateHomepageProduct($product);
			}
		}
		else
		{
			?>
			<p>Không có sản phẩm</p>
			<?php
		}
		
		?>
        
        </div><!-- end div sappham_box -->
    	<div class="hompage_promote_box">
        	<h2><?php echo anchor(base_url()."news/1/0", 'Khuyến mãi') ?></h2>
        <?php
		$products = $this->product_model->getRandomPromotionProducts(4);
		if(count($products) > 0)
		{
			$this->generateListProducts($products);
		}
		else
		{
			?>
			<p>Không có sản phẩm</p>
			<?php
		}
		
		?>
        </div>
		<div class='clear'></div>
		</div>
        <div id="rightContainer"><?php $this->generateRightPaneInfo() ?></div>
    		<div class="both"></div>
		</div> <!-- Close center tag -->
        <?php
	}
	
	private function generateRightPaneInfo(){
	?>
    <div class="hotline_box">
        	<div class="rightcontent_box">            	
                <div class="hotline_title">HOTLINE</div>
                <div class="hotline_info">0983 553 977</div>
                <div class="title">Liên hệ nhanh</div>
                <div class="info">
                	<span class="phone_name">
                Mirror shop</span>
                	<p>
                <a href="ymsgr:sendim?mirrorcomestic"><IMG border=0 alt="Chat with me" src="http://opi.yahoo.com/online?u=mirrorcomestic&amp;m=g&amp;t=2" modo="false"></a>
                	</p>
                 </div><!-- end div info -->                
                 
                 <?php
					$products = $this->product_model->getRandomBestSellerProducts(4);
					if(count($products) > 0)
					{
					?>
                    	
                    <?php
						$count = 1;
						foreach($products as $product)
						{							
							if($count++ == 1){
								$this->generateBestSellerProduct($product, true);
							}else{
								$this->generateBestSellerProduct($product, false);
							}
						}
						?>
                        <?php
					}
				
				?>
                
                <div class="hotline_title">THANH TOÁN</div>
                <div class="payment_info">
                    <p>- Thanh toán trực tiếp.</p>

                    <p>- Thanh toán thông qua tài khoản:</p>
                    
                    <p>Chủ tài khoản 
                    <b> Đoàn Thị Minh Thái </b></p>
                    
                    <p>Tài khoản Vietcombank
                    <b> 0251001872646 </b></p>
                </div>
             </div><!-- end div rightcontent_box -->
        </div><!-- end div hotline_box -->
    <?php
	}
	
	private function generateBestSellerProduct($product, $visible)
	{
		if($visible){
		?>
			<div class="markproduct_info">
        <?php 
		}else{
		?>
        	<div class="markproduct_info" style="display:none;">
        <?php
		}
		$marks = $product->getMarks();
		echo '<div class="hotline_title">'.strtoupper($marks[0]->getTitle()).'</div>';
		
		$this->generateListProducts($product);
		?>            
        </div><!-- end div markproduct_info -->
        <?php
	}
	
	private function generateProductType_ProductCenter($category, $productType = NULL, $products, $paging_link, $error = ERROR_NONE)
	{
	?>
		<div class='center'> <!-- Open center tag -->
			<div id='bodyContainer'> <!-- Open bodyContainer tag -->
		<?php
		if ($productType->getId() > 0)
		{
			$this->generateNavigationLink($category, $productType); // Navigation bar
			
			if (count($products) > 0)
			{
				?>
				<h2><?php echo $productType->getTitle() ?></h2>
				<?php
				
				$this->generateListProducts($products);
			}
			else
			{
				?>
				<p>Không có sản phẩm</p>
				<?php
			}
		}
		else
		{
			if ($error = NOT_EXISTED_ERROR)
			{
			?>
			<h2 style="color: red"><?php echo NOT_EXISTED_ERROR_TEXT ?></h2>
			<?php
			}
		}
		?>
		
		<!-- Paging here -->
		<?php echo $paging_link ?>
		
		<div class='clear'></div>
		</div>
        <div id="rightContainer"><?php $this->generateRightPaneInfo() ?></div>
    		<div class="both"></div>
		</div> <!-- Close center tag -->
	<?php
	}
	
	private function generateCategoryCenter()
	{
	?>
		<div class='center'> <!-- Open center tag -->
			<div id='bodyContainer'> <!-- Open bodyContainer tag -->
		
		<!-- Navigation -->
		<?php
		$link = "<div class='crumb_nav'>".anchor('', 'Home');
		$link .= " &gt;&gt; Sản Phẩm";
		$link .= "</div>";			
		echo $link;
		?>
		
		<h2>Danh mục sản phẩm</h2>
		
		<?php
		$categories = Category_Model::getInstance()->getStorageCategories();
		foreach ($categories as $category)
		{
			?>
			<div class="row">
        		<div id="cate_box" class="boxed2">			
            	<div class="cate_image">
            		<?php
            		$img = array(	'style'		=> 	'VISIBILITY: visible',
									'class'		=>	'browseProductImage',
									'title'		=>	'',
									'border'	=> 0,
									'alt'		=> '',
									'src'		=> StringUtils::getInstance()->decodeHTMLString($category->getImage()),
									"onerror"=> "this.src='".base_url()."resources/products/noimage.jpg'");
					echo img($img);
            		?>
            	</div>
				<div class="cate_content">            	
					<div class="cate_title">
						<?php echo anchor(base_url().'category/view/'.$category->getId(), $category->getTitle()) ?>
					</div>
					<div class="cate_description">
						<?php
						echo ellipsize($category->getDescription(), CHAR_LIMIT, 1);
						echo anchor('category/view/'.$category->getId(), 'Xem thêm');
						?>
					</div><!-- end div cate_description -->  
            	</div><!-- end div cate_content -->  
            	<div class=clear></div>
				</div><!-- end div cate_box -->  
        	</div><!-- end div row -->
        <?php
		}
		?>
				
		<div class='clear'></div>
		</div>
        <div id="rightContainer"><?php $this->generateRightPaneInfo() ?></div>
    		<div class="both"></div>
		</div> <!-- Close center tag -->
	<?php
	}

	private function generateNewsCenter($newsarr, $paginationLinks, $promotion = 0)
	{
	?>
		<div class='center'> <!-- Open center tag -->
			<div id='bodyContainer'> <!-- Open bodyContainer tag -->
		
		<!-- Navigation -->
		<?php
		$title = $promotion ? "Khuyến mãi" : "Làm đẹp";
		
		$link = "<div class='crumb_nav'>".anchor('', 'Home');
		$link .= " &gt;&gt; ";
		$link .= $title;
		$link .= "</div>";			
		echo $link;
		?>
		
		<h2>Tin tức <?php echo $title ?></h2>
		
		<?php
		foreach ($newsarr as $news)
		{
			?>
			<div class="row">
        		<div id="cate_box" class="boxed2">			
            	<div class="cate_image">
            		<?php
            		$img = array(	'style'		=> 	'VISIBILITY: visible',
									'class'		=>	'browseProductImage',
									'title'		=>	'',
									'border'	=> 0,
									'alt'		=> '',
									'src'		=> StringUtils::getInstance()->decodeHTMLString($news->getImage()),
									"onerror"	=> "this.src='".base_url()."resources/products/noimage.jpg'");
					echo img($img);
            		?>
            	</div>
				<div class="cate_content">            	
					<div class="cate_title">
						<?php echo anchor(base_url().'news/view/'.$news->getId(), $news->getTitle()) ?>
					</div>
					<div class="cate_description">
						<?php
						$config = HTMLPurifier_Config::createDefault();
						$purifier = new HTMLPurifier($config);
						$clean_html = $purifier->purify(StringUtils::getInstance()->decodeHTMLString($news->getContent()));
						$str = strip_tags($clean_html);
						
						$s = str_replace(array("\r\n", "\r", "\t"), "", $str);
						$s = preg_replace('/\s+/', ' ', $str);
						echo mb_substr($s, 0, CHAR_LIMIT);
						echo anchor('news/view/'.$news->getId(), 'Xem thêm');
						?>
					</div><!-- end div cate_description -->  
            	</div><!-- end div cate_content -->  
            	<div class=clear></div>
				</div><!-- end div cate_box -->  
        	</div><!-- end div row -->
        <?php
		}
		?>
		
		<!-- Paging here -->
		<?php echo $paginationLinks ?>	
				
		<div class='clear'></div>
		</div>
        <div id="rightContainer"><?php $this->generateRightPaneInfo() ?></div>
    		<div class="both"></div>
		</div> <!-- Close center tag -->
	<?php
	}
	
	private function generateCategory_ProductTypeCenter($category, $producttypes, $error = ERROR_NONE)
	{
		?>
		<div class='center'> <!-- // Open center tag -->
			<div id='bodyContainer'> <!-- // Open bodyContainer tag -->
		
		<?php
		if ($category->getId() > 0)
		{
			$this->generateNavigationLink($category); // Navigation bar
			?>
			<h2><?php echo $category->getTitle() ?></h2>
			<?php
			
			if (count($producttypes) > 0)
			{
				foreach ($producttypes as $producttype)
				{
					?>
				<div class="cate_title">
					<?php echo anchor(base_url().'producttype/view/'.$producttype->getId(), $producttype->getTitle()) ?>
				</div>
				<div>
					<?php
					$products = $this->product_model->getProductsByProductTypeId($producttype->getId(), 0, 4);//get 3 first product to display example
					if(count($products) > 0)
					{
						$this->generateListProducts($products);
					}
					else
					{
						?>
						<p>Không có sản phẩm</p>
						<?php
					}
				?>
				</div>
                <div class='clear'></div>
				<?php
				}
			}
			else
			{
				?>
				<p>Không có sản phẩm</p>
				<?php
			}
		}
		else
		{
			$errorText = "";
			
			if ($error = NOT_EXISTED_ERROR)
				$errorText = NOT_EXISTED_ERROR_TEXT;
			
			?>
			<h2 style="color: red"><?php echo $errorText ?></h2>
			<?php
		}
		?>	<div class='clear'></div>			
			</div>
            <div id="rightContainer"><?php $this->generateRightPaneInfo() ?></div>
    		<div class="both"></div>
		</div> <!-- // Close center tag -->
		<?php
	}	
	
	private function generateCenterProductDetail($category = NULL, $productType = NULL, $product = NULL, $related_products = NULL, $error = ERROR_NONE)
	{
		?>
		<div class='center'>  <!-- Open center tag -->
			<div id='bodyContainer'> <!-- Open bodyContainer tag -->
		
		<?php
		if ($error == ERROR_NONE)
		{
		$this->generateNavigationLink($category, $productType, $product); // Navigation bar
		$this->generateProductDetail($product, $related_products);	
		}
		else
		{
			if ($error == NOT_EXISTED_ERROR)
			{
		?>
			<h2 style="color: red"><?php echo NOT_EXISTED_ERROR_TEXT ?></h2>
		<?php
			}
		}		
		?>
		
			<div class='clear'></div>
			</div>
            <div id="rightContainer"><?php $this->generateRightPaneInfo() ?></div>
    		<div class="both"></div>
		</div>	<!-- Close center tag -->
		<?php
	}
	
	
	
	public function generateFooter()
	{
		?>
		<div id='footermainPan'>
		  <div id='footerPan'>  
			<ul class='templateworld'>
		        <li>Design By:</li>
		        <li><a href='#' target='_blank'> Normal group</a></li>
		  	</ul>
		  </div>
		</div>
		<?php
	}
	
	private function generateHomepageProduct($product){
		?>
        <div class="row">
        	<div class="productdetails-view">
        	<div>
            	<div><!-- start div product title -->                	
                	<div class="product_image floatleft">
                    	<div class="main-image">                        	
							<?php
							$img = array(	"style"	=> "VISIBILITY: visible",
											"class"	=> "browseProductImage",
											"title"	=> $product->getName(),
											"border"=>	0,
											"alt"	=> $product->getName(),
											"src"	=> $product->getImage(),
											"height"=> 140,
											"width"	=>	140,
											"onerror"=> "this.src='".base_url()."resources/products/noimage.jpg'");
							?>
							<?php echo anchor(base_url()."product/view/".$product->getId(), img($img), array("class" => "modal")) ?>
                        </div>                        
                    </div><!-- end div product_image -->
                    <div class="product-top">                    	
						<h2>
	                	<?php echo anchor(base_url()."product/view/".$product->getId(), $product->getName()) ?>
	                </h2>
                    </div>                    
                    <div class="pricesalesPrice">Giá bán: 
                    	<span class="pricesalesPrice"><?php echo number_format($product->getPrice(), 2, '.', ','); ?> ₫</span>
                    </div>
                </div><!-- end div product title -->
                <div class="clear"></div>
            </div>           
        </div><!-- end div productdetails-view -->
        </div><!-- end div row -->
        <?php		
	}
	
	private function generateProduct($product, $showInfo = true)
	{
		?>
	    	<div class='product'>	
	        	<div class='spacer'>
		        	<div class='product_image'>
		        		<?php
		        		$img = array(	"style"	=> "VISIBILITY: visible",
										"class"	=> "browseProductImage",
										"title"	=> $product->getName(),
										"border"=>	0,
										"alt"	=> $product->getName(),
										"src"	=> $product->getImage(),
										"height"=> 140,
										"width"	=>	140,
										"onerror"=> "this.src='".base_url()."resources/products/noimage.jpg'");
		        		?>
	                	<?php echo anchor(base_url()."product/view/".$product->getId(), img($img), array("class" => "modal")) ?>
	                </div>
	                <h2>
	                	<?php echo anchor(base_url()."product/view/".$product->getId(), $product->getName()) ?>
	                </h2>
	                <div>
	                	<div class='product-price'>
	                    	<div class='priceText'>Giá bán: <span class='pricetag'><?php echo number_format($product->getPrice(), 2, '.', ','); ?> ₫</span></div>
                            <div class='priceText'>Xuất xứ: <span class='pricetag'><?php echo $product->getBranch()->getName() ?> </span></div>
	                    </div>
	                </div>
                    <?php if($showInfo){
	                echo "<p>".anchor(base_url()."product/view/".$product->getId(), "Xuất xứ", array("class" => "product-details")) ."
	                </p>";}
					?>
	            </div>
	        </div>        
      <?php
	}
	
	private function generateProductDetail($product, $related_products = NULL)
	{
	?>
    	<div class="productDetail">
		<div class="productdetails-view">
        	<div>
            	<div><!-- start div product title -->
                	<div class="product_image floatleft">
                    	<div class="main-image">
                    		<?php
                    			$img = array(	'style'		=>	'VISIBILITY: visible',
												'class'		=>	'browseProductImage',
												'title'		=>	$product->getName(),
												'border'	=>	0,
												'alt'		=>	$product->getName(),
												'src'		=> $product->getImage(),
												"onerror"	=> "this.src='".base_url()."resources/products/noimage.jpg'");
								echo img($img);
                    		?>
                        </div>
                        <span class="main-image-des"><?php echo $product->getName() ?></span>
                    </div><!-- end div product_image -->
                    <div class="product-top">                    	
						<h1><?php echo htmlspecialchars_decode($product->getName()) ?></h1>
                    </div>                    
                    <div class="pricesalesPrice">Giá bán: 
                    	<span class="pricesalesPrice"><?php echo number_format($product->getPrice(), 2, '.', ','); ?> ₫</span>
                    </div>
                </div><!-- end div product title -->
                <div class="detailText"> <!-- start div content -->
					<?php echo StringUtils::getInstance()->decodeHTMLString(($product->getDescription())) ?>
                    </div><div>
					<?php
					if ($related_products != NULL)
					{
						if (count($related_products) > 0)
						{
							?>
                            <div id="sameProductType">
							
							</div>
                            <div class="hompage_product_box">
                                <h2><?php echo anchor(base_url()."category", 'Sản phẩm cùng loại') ?></h2>
                            							
							<?php
							foreach($related_products as $product)
							{
								$this->generateHomepageProduct($product);
							}
							?>
                                                       
                            </div><!-- end div sappham_box -->
                            <div class="hompage_promote_box">
                                <h2><?php echo anchor(base_url()."news/1/0", 'Khuyến mãi') ?></h2>
                            <?php
                            $products = $this->product_model->getRandomPromotionProducts(4);
                            if(count($products) > 0)
                            {
                                $this->generateListProducts($products);
                            }
                            else
                            {
                                ?>
                                <p>Không có sản phẩm</p>
                                <?php
                            }
                            
                            ?>
                            </div>
                            <?php
						}
					}
					?>
                </div><!-- end div content -->
                <div class="clear">
                </div>
            </div>
            <div class="product-description">
            </div><!-- end div product-description -->
        </div><!-- end div productdetails-view -->
        </div>
    <?php
	}
	
	public function generateUserInfo($userdata)
	{
		if ($userdata != NULL)
		{
		?>
		<div id='userinfo'>
			<?php echo anchor('admin/view/', $userdata['username']) ?></br>
			<?php echo anchor('admin/logout', 'Đăng xuất') ?>
		</div>
		<?php
		}
	}
	
	public function isAdmin()
	{
		$userdata = $this->session->userdata('userdata');
		
		if ($userdata != NULL)
		{
			return $userdata['admin'];
		}
		
		return FALSE;
	}
	
	public function generateAdminLeftMenu()
	{
	?>
		<div class="sidebar" id="sideLeft">
			<p><?php echo anchor('category', 'Quản lý Danh mục sản phẩm') ?></p>
			<p><?php echo anchor('producttype', 'Quản lý Nhóm sản phẩm') ?></p>
			<p><?php echo anchor('product', 'Quản lý Sản phẩm') ?></p>
			<p><?php echo anchor('product/search', 'Tìm kiếm Sản phẩm') ?></p>
			<p><?php echo anchor('mark', 'Quản lý Nhãn sản phẩm') ?></p>
			<p><?php echo anchor('branch', 'Quản lý Xuất xứ') ?></p>
			<p><?php echo anchor('news', 'Quản lý Tin tức') ?></p>
			<p><?php echo anchor('info', 'Quản lý Thông tin liên hệ') ?></p>
		</div>
	<?php
	}
}
/* End of file page.php */
/* Location: ./application/views/components/page.php */